#!/bin/sh

function usage()
{
    echo "自动构建脚本需要2个参数 第一个参数是环境 第二个是镜像的目录项目名称"
    echo "    环境参数任意一个  dev test simu prod"
    echo "    镜像名称是完整的目录、项目、分支等，如 admin-go-tpl:user_archive"
    echo "    镜像名称后缀自动加入当前时间"
    echo "    例如: sh auto_docker.sh test admin-go-tpl:user_archive"
    echo "    镜像地址: image.docker.dahai.com/apps/admin-go-tpl:user_archive-20190213192011"
    exit
}

if [ $# != 2 ]
then
    usage
fi


OCEAN_ENV=$1
IMAGE_NAME=$2
if [[ $OCEAN_ENV != "dev" && $OCEAN_ENV != "test" && $OCEAN_ENV != "simu" && $OCEAN_ENV != "prod" ]]
then
    echo "环境参数请选择任意一个： dev test simu prod"
    exit
fi


# 开发环境
if [[ $OCEAN_ENV == "dev" ]]
then
    echo "开发环境目前不需要构建镜像，只替换配置文件"
    cp environments/dev/app.conf conf/
    exit
fi

# 测试环境
if [[ $OCEAN_ENV == "test" ]]
then
    IMAGE_URL=image.docker.dahai.com/apps/${IMAGE_NAME}-$(date "+%Y%m%d%H%M%S")

    echo "构建测试环境镜像，请确认以下内容是否正确"
    echo "  环境："$OCEAN_ENV
    echo "  镜像地址："$IMAGE_URL
    read -p "是否构建？[y/n]" input
    if [ $input != "y" ]
    then
        exit
    fi

    echo "开始构建镜像"
    docker build -t $IMAGE_URL .

    echo "镜像推送仓库"
    docker push $IMAGE_URL
fi

# 仿真环境
if [[ $OCEAN_ENV == "simu" ]]
then
   echo "仿真环境需要去开发者平台构建"
   exit
fi

# 生产环境
if [[ $OCEAN_ENV == "prod" ]]
then
    echo "生产环境需要去开发者平台构建"
    exit
fi
